package com.noble.meether.fragment.mail.panel;

import android.content.Context;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.noble.glamour.module.logic.application.ModuleMgr;
import com.noble.glamour.module.util.TimeUtil;
import com.noble.glamour.module.util.UIShow;
import com.noble.library.image.ImageLoader;
import com.noble.library.widget.RecyclerView.base.BaseRecyclerViewAdapter;
import com.noble.library.widget.RecyclerView.base.BaseRecyclerViewHolder;
import com.noble.meether.R;
import com.noble.meether.chat.unread.UnreadMgrImpl;
import com.noble.meether.chat.unread.view.BadgeView;
import com.noble.meether.fragment.mail.bean.UserMessage;

/**
 * mailFragment消息列表
 */
public class MailMsgAdapter extends BaseRecyclerViewAdapter<UserMessage> {
    private Context mContext;

    public MailMsgAdapter(Context mContext) {
        this.mContext = mContext;
    }

    @Override
    public int[] getItemLayouts() {
        return new int[]{R.layout.item_msg_panel};
    }

    @Override
    public void onBindRecycleViewHolder(BaseRecyclerViewHolder viewHolder, int position) {
        MyViewHolder holder = new MyViewHolder(viewHolder);
        final UserMessage data = getItem(position);
        ImageLoader.loadCircleAvatar(mContext, data.getAvatar(), holder.iv_avatar);
        holder.tv_name.setText(data.getNickName());
        holder.user_vip.setVisibility(data.isVip() ? View.VISIBLE : View.GONE);

        // 未读角标
        final long uidTo = data.getWhisperID();
        final int onlineNum = ModuleMgr.getUnreadMgr().registerMsgBadge(holder.mail_number, false, uidTo);

        // 最新消息内容
        if (TextUtils.isEmpty(data.getContent())) {
            holder.mail_content.setVisibility(View.GONE);
        } else {
            holder.mail_content.setVisibility(View.VISIBLE);
            holder.mail_content.setText(data.getContent());
        }

        // 最新消息时间
        if (data.getUpdateTime() <= 0) {
            holder.mail_time.setVisibility(View.GONE);
        } else {
            holder.mail_time.setVisibility(View.VISIBLE);
            holder.mail_time.setText(TimeUtil.formatBeforeTime(data.getUpdateTime()));
        }

        holder.rl_item.setOnClickListener(view -> {
            UIShow.showPrivateChatAct(mContext, uidTo, data.getNickName(), data.getAvatar(), data.isVip());

            // 未读角标
            if (onlineNum > 0) {
                refreshBadge(uidTo, holder.mail_number);
                ModuleMgr.getUnreadMgr().reduceUnreadByKey(UnreadMgrImpl.MSG_NOTICE, onlineNum);
            }
        });
    }

    private void refreshBadge(long uidKey, BadgeView view) {
        view.setVisibility(View.GONE);
        ModuleMgr.getUnreadMgr().resetMsgUnreadByKey(uidKey);
        notifyDataSetChanged();
    }

    class MyViewHolder {
        RelativeLayout rl_item;
        TextView tv_name, mail_content, mail_time;
        ImageView iv_avatar, user_vip;
        BadgeView mail_number;

        public MyViewHolder(BaseRecyclerViewHolder convertView) {
            initView(convertView);
        }

        private void initView(BaseRecyclerViewHolder convertView) {
            rl_item = convertView.findViewById(R.id.rl_item);
            iv_avatar = convertView.findViewById(R.id.icon_friend);
            tv_name = convertView.findViewById(R.id.user_nick);
            mail_number = convertView.findViewById(R.id.mail_number);
            user_vip = convertView.findViewById(R.id.user_vip);
            mail_content = convertView.findViewById(R.id.mail_content);
            mail_time = convertView.findViewById(R.id.mail_time);
        }
    }

    @Override
    public int getRecycleViewItemType(int position) {
        return 0;
    }
}